package com.wei.duowan.bbs.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.presenter.BasePresenter;
import com.wei.duowan.bbs.utils.ActivityManager;
import com.wei.duowan.bbs.utils.RxUtils;

import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * 作者：赵若位
 * 时间：2019/7/31 23:26
 * 联系方式：1070138445
 * 功能：
 */
public abstract class BaseActivity<T extends BasePresenter> extends RxAppCompatActivity
{
    public static final String TAG = BaseActivity.class.getSimpleName();

    protected abstract T createPresenter();

    protected abstract int getLayoutID();

    protected abstract void initView();

    private T mPresenter = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        ActivityManager.getManager().addActivity(this);
        super.onCreate(savedInstanceState);
        mPresenter = createPresenter();
        if (getLayoutID() > 0)
        {
            setContentView(getLayoutID());
        }
        ButterKnife.bind(this);
        setStatusColor();
        initView();
    }


    @Override
    public void setContentView(int layoutResID)
    {
        super.setContentView(layoutResID);
    }


    private static long exitTime = 0;//点击后退记录时间

    @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (this instanceof MainActivity && event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            if ((System.currentTimeMillis() - exitTime) > 2000)
            {
                alert(getString(R.string.finish_app_click_agin));
                exitTime = System.currentTimeMillis();
            } else
            {
                ActivityManager.getManager().exit(getApplicationContext());
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    /*设置状态栏的样式*/
    public void setStatusColor()
    {
        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true).init();
    }

    @Override
    public void finish()
    {
        super.finish();
        overridePendingTransition(R.anim.anima_right_in, R.anim.anima_left_out);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        ButterKnife.unbind(this);
        ActivityManager.getManager().finishActivity(this);
    }

    /*Toast弹窗*/
    public void alert(@Nullable String msg)
    {
        if (!TextUtils.isEmpty(msg))
        {
            runOnUiThread(() -> Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT).show());
        }
    }


    /*在Include中设置标题*/
    protected void setTitle(@Nullable String msg)
    {
        if (!TextUtils.isEmpty(msg) && findViewById(R.id.tv_title) != null)
        {
            TextView tv_title = findViewById(R.id.tv_title);
            tv_title.setText(msg);
        }
    }

    /*在Include中设置标题*/
    public void setTitle(@StringRes int resourceId)
    {
        this.setTitle(App.getResource().getString(resourceId));
    }

    /*返回按钮监听*/
    protected void setFinish()
    {
        ImageView imgFinish = findViewById(R.id.img_finish);
        if (imgFinish != null)
        {
            imgFinish.setVisibility(View.VISIBLE);
            imgFinish.setOnClickListener(view -> doFinish());
        }
    }

    /*Intent启动Activity不传递数据*/
    public void openActivity(Class<? extends AppCompatActivity> cls)
    {
        this.openActivity(cls, null);
    }

    /*Intent启动Activity并传递数据*/
    public void openActivity(Class<? extends AppCompatActivity> cls, Parcelable data)
    {
        Intent it = new Intent(this, cls);
        if (data != null)
        {
            it.putExtra(cls.getSimpleName(), data);
        }
        startActivity(it);
        overridePendingTransition(R.anim.anima_left_in, R.anim.anima_right_out);
    }


    /*延迟关闭Activity*/
    public void doFinish(long delay)
    {
        Observable.empty()
                .delay(delay, TimeUnit.MILLISECONDS)
                .compose(RxUtils.getSchedulers())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .doFinally(() -> finish())
                .subscribe();
    }

    /*默认关闭Activity*/
    public void doFinish()
    {
        this.doFinish(200);
    }
}
